package gui.customizer.panes.logs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.SSHConnection;
import net.SSHConnectionProxy;
import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;

public class Kernel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1963899239086945063L;
	private JButton btnGetLogs;
	private IptablesLogsArea logs;
	private Filters filters;

	/**
	 * Create the panel.
	 */
	public Kernel() {
		setLayout(new MigLayout("", "[grow][grow]", "[80:n][80px:n][grow]"));
		
		// Get Panel
		JPanel panelGetLogs = new JPanel();
		panelGetLogs.setLayout(new MigLayout("", "[]", "[]"));
		btnGetLogs = new JButton("Get logs");
		panelGetLogs.add(btnGetLogs, "cell 0 0");
		
		JScrollPane getLogsScroller = new JScrollPane(panelGetLogs);
		getLogsScroller.setBorder(BorderFactory.createTitledBorder("Download Filtered Logs"));
		add(getLogsScroller, "cell 1 0,grow");
		
		// Filters Panel
		filters = new Filters();
		JScrollPane filtersScroller = new JScrollPane(filters);
		filtersScroller.setBorder(BorderFactory.createTitledBorder("Filters"));
		add(filtersScroller, "cell 0 0 1 2,grow");
		
		// IPtables Logs
		logs = new IptablesLogsArea();
		logs.setBorder(BorderFactory.createTitledBorder("Filtered Logs"));
		add(logs, "cell 0 2 2 1,grow");
		
		initialize();

	}
	
	private void initialize() {
		final SSHConnection conn = new SSHConnectionProxy();
		
		btnGetLogs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!SSHConnectionProxy.isConnected())
					return;
				try {
					logs.setLines(conn.getIptablesLog(filters.getFilterString()));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}
